<?php

namespace App\Http\Controllers;

use App\Models\Course;
use App\Models\CoursesSection;
use Illuminate\Http\Request;
use App\Models\Directory;
use App\Models\CourseCollect;
class CourseController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param = $request->input() ?: [];
        $course = (new Course())->getList($param);
        return  $this->ResultJson(1,'获取成功!',$course);
    }




    /****
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 出院须知
     */
    public function leaveHospital(Request $request)
    {
        $param = $request->input() ?: [];
        $course = (new Course())->leaveHospital($param);
        return $this->ResultJson(1, '获取成功!', $course);
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     *
     * 课程收藏
     */
    public function courseCollect(Request $request)
    {
        $param = $request->input() ?: [];
        if(empty($param['course_id'])){
            return  $this->ResultJson(0,'课程ID不能为空!',[]);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        if (empty($param['collect_type'])) {
            return $this->ResultJson(0, '收藏类型不能为空!', []);
        }
        $course = (new CourseCollect())->setCourseCollect($param);
        switch ($course) {
            case 1:
                return  $this->ResultJson(1,'取消收藏成功!',[]);
                break;
            case 2:
                return  $this->ResultJson(0,'取消收藏失败!',[]);
                break;
            case 3:
                return  $this->ResultJson(1,'收藏成功!',[]);
                break;
            case 4:
                return  $this->ResultJson(0,'收藏失败!',[]);
                break;
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取收藏课程列表
     */
    public function getCollectList(Request $request)
    {
        $data = $request->input();
        if (empty($data['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($data['collect_type'])) {
            return $this->ResultJson(0, '用户或者医护类型不能为空!', []);
        }
//        $user_id = $request->get('user_id');
        $list = (new CourseCollect())->getCollectListData($data);
        if (!empty($list)) {
            return $this->ResultJson(1, '获取成功!', $list);
        }
        return $this->ResultJson(0, '暂无数据!', []);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取收藏课程列表Android使用
     */
    public function getCollectListDataAndroid(Request $request)
    {
        $data = $request->input();
        if (empty($data['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($data['collect_type'])) {
            return $this->ResultJson(0, '用户或者医护类型不能为空!', []);
        }
//        $user_id = $request->get('user_id');
        $list = (new CourseCollect())->getCollectListDataAndroid($data);
        if (!empty($list)) {
            return $this->ResultJson(1, '获取成功!', $list);
        }
        return $this->ResultJson(0, '暂无数据!', []);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $param = $request->input() ?: [];
        if(empty($param['course_id'])){
            return  $this->ResultJson(0,'课程id不能为空!',[]);
        }
        if(empty($param['work_id'])){
            return  $this->ResultJson(0,'工作室id参数错误!',[]);
        }
        $course = (new Course())->getCourseDetail($param);
        if(is_array($course)){
            return  $this->ResultJson(1,'获取成功!',$course);
        }
        return $this->ResultJson(0,$course,[]);

    }


    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function chapterDetail(Request $request)
    {
        $param = $request->input() ?: [];
        if (empty($param['chapter_id'])) {
            return $this->ResultJson(0, '章节ID不能为空!', []);
        }
//        if (empty($param['wc_relate_id'])) {
//            return $this->ResultJson(0, '关联ID不能为空!', []);
//        }
        $course = (new Directory())->getChapterInfo($param);
        return $this->ResultJson(1, '获取成功!', $course);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Course  $course
     * @return \Illuminate\Http\Response
     * 发布科普视频IOS
     */
    public function addCoursesIos(Request $request)
    {
        $param = $request->input();
        $yes = $this->validateParam($param);
        if(empty($param['section'])){
            return $this->ResultJson(0, "章节不能为空", []);
        }
        if ($yes === true) {
            $res = (new Course())->doAddCoursesIos($param);
            if (is_array($res)) {
                if (isset($param['id'])) {
                    return $this->ResultJson(1, "编辑成功", $res);
                }
                return $this->ResultJson(1, "添加成功", $res);
            }
        }
    }



    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Course  $course
     * @return \Illuminate\Http\Response
     * 发布科普视频
     */
    public function addCourses(Request $request)
    {
        $param = $request->input();
        if (isset($param['id'])) {
            $yes = true;
        } else {
            $yes = $this->validateParam($param);
        }
        if ($yes === true) {
            $res = (new Course())->doAddCourses($param);
            if (is_array($res)) {
                if (isset($param['id'])) {
                    return $this->ResultJson(1, "编辑成功", $res);
                }
                return $this->ResultJson(1, "添加成功", $res);
            }
        }
        return $this->ResultJson(0, $yes, []);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加科普视频章节
     */
    public function addCourseSection(Request $request)
    {
        $param = $request->input();
        if (empty($param['courses_title'])) {
            return $this->ResultJson(0, "章节名称不能为空", []);
        }
        if (empty($param['courses_video'])) {
            return $this->ResultJson(0, "视频连接不能为空", []);
        }
        if (empty($param['courses_id'])) {
            return $this->ResultJson(0, "课程ID不能为空", []);
        }
        $res = (new CoursesSection())->doAddCoursesSection($param);
        if (is_array($res)) {
            if (isset($param['id'])) {
                return $this->ResultJson(1, "编辑成功", $res);
            }
            return $this->ResultJson(1, "添加成功", $res);
        }
        return $this->ResultJson(0, $res, []);
    }

    /***
     * @param $res
     * @return bool|string
     * 验证参数
     */
    public function validateParam($res)
    {
        if (empty($res['add_user_id'])) {
            return '用户id不能为空!';
        }
        if (empty($res['course_sort_id'])) {
            return '课程分类id不能为空!';
        }
        if (empty($res['self_cate_id'])) {
            return '工作室自己分类id不能为空!';
        }
        if (empty($res['is_original'])) {
            return '是否原创不能为空!';
        }
        if (empty($res['doc_studio_id'])) {
            return '工作室ID不能为空!';
        }
        if (empty($res['course_title'])) {
            return '课程标题不能为空!';
        }
        if (empty($res['course_img'])) {
            return '课程封面图不能为空!';
        }
        if (empty($res['course_info'])) {
            return '课程简介不能为空!';
        }
//        if (empty($res['explain'])) {
//            return '课程说明不能为空!';
//        }
        return true;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 视频库
     */
    public function getAllCoursesList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        $res = (new Course())->getAllCoursesList($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据!', []);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *课程提交选单
     */

    public function addRelateCourses(Request $request)
    {
        $param = $request->input();
        if(empty($param['work_id'])){
            return  $this->ResultJson(0,'工作ID不能为空!',[]);
        }
        if (empty($param['cate_id'])) {
            return $this->ResultJson(0, '分类ID不能为空!', []);
        }
        if (empty($param['course_ids'])) {
            return $this->ResultJson(0, '文章ID不能为空!', []);
        }
        $course = (new Course())->addRelateCourses($param);
        if($course === true){
            return  $this->ResultJson(1,'添加成功!',[]);
        }
        return  $this->ResultJson(0,'添加失败!',[]);
    }

    /**
     * 医护端课程详情
     */
    public function getCourseDetailForDoctor(Request $request)
    {
        $param = $request->input();
        if (empty($param['course_id'])) {
            return $this->ResultJson(0, '课程ID不能为空!', []);
        }
        $res = (new Course())->getCourseInfo($param);
        return $this->ResultJson(1, '获取成功!', $res);
    }

    /**
     * 医护端我的课程
     */
    public function myCourses(Request $request)
    {
        $param = $request->input();
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $res = (new Course())->getMyCourses($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据!', []);
    }

   /**
     * 共享和取消共享视频
     */
    public function shareCourses(Request $request)
    {
        $param = $request->input();
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        if (empty($param['course_id'])) {
            return $this->ResultJson(0, '课程ID不能为空!', []);
        }
        $res = (new Course())->shareCourses($param);
        if (is_numeric($res)) {
            if ($res == 1) {
                return $this->ResultJson(1, '取消共享成功!', ['data' => $res]);
            } elseif ($res == 2) {
                return $this->ResultJson(1, '取消共享失败!', ['data' => $res]);
            } elseif ($res == 3) {
                return $this->ResultJson(1, '共享成功!', ['data' => $res]);
            } elseif ($res == 4) {
                return $this->ResultJson(1, '共享失败!', ['data' => $res]);
            }
        }
        return $this->ResultJson(0, $res, []);
    }
    
    /**
     * 
     * 删除课程
     */
    public function delCourse(Request $request)
    {
        $param = $request->input();
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        if (empty($param['course_id'])) {
            return $this->ResultJson(0, '课程ID不能为空!', []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $res = (new Course())->del($param);
        if ($res === true) {
            return $this->ResultJson(1, '删除成功!', []);
        }
        return $this->ResultJson(0, $res, []);
    }




}
